﻿using System;
using Microsoft.Office.Interop.Excel;
using System.Reflection;
using System.Diagnostics;

namespace TecBaseObjects.Base {
    public class ProjektArSheet {

        #region Members

        Worksheet mySheet;
        int nextInvoiceNumber = 0;

        #endregion

        #region Constructor

        public ProjektArSheet(Worksheet sh) {
            this.mySheet = sh;
        }

        #endregion

        #region Properties

        public int NewInvoiceNumber {
            get {
                return this.nextInvoiceNumber;
            }
            set {
                this.nextInvoiceNumber = value;
            }
        }

        #endregion

        #region Functions

        public int GetNextFreeRowNumber(int jahr, int monat) {

            int ret = 0;
            int currentRow = 2;
            int currentMonth = 1;
            bool weiter = false;
            Range r1;
            int maxNr = 0;

            try {
                while (weiter == false & currentRow < 2000) {
                    //Set Rows
                    Range c1 = mySheet.Cells[currentRow, "L"];
                    Range c2 = mySheet.Cells[currentRow, "M"];
                    DateTime dDate;
                    if (c1.Value != null) {
                        if (DateTime.TryParse(c1.Value.ToString(), out dDate)) {
                            int yy;
                            if (c2.Value == null) {

                                throw new Exception("Keine vorgehende RechnungsNummer!");
                            }
                            if (int.TryParse(c2.Value.ToString(), out yy)) {
                                maxNr = yy;
                            }
                            currentMonth = dDate.Month;
                        } else {
                            int yy;
                            if (int.TryParse(c2.Value.ToString(), out yy)) {
                                if (yy > 12) {
                                    return 0;
                                }
                            }

                            if (c1.Value != null) {
                                if (c1.Value.Contains("Summe") && c2.Value == monat) {
                                    r1 = mySheet.Rows[currentRow - 1, Missing.Value];
                                    r1.Select();
                                    r1.Copy();
                                    r1.Insert(Shift: XlInsertShiftDirection.xlShiftDown);
                                    Range rn = mySheet.Cells[currentRow - 1, "M"];
                                    if (rn.Value != null) {
                                        int newInvNo;
                                        if (int.TryParse(rn.Value.ToString(), out newInvNo)) {
                                            NewInvoiceNumber = (maxNr >= newInvNo ? maxNr + 1 : newInvNo + 1);
                                        }

                                    }
                                    return currentRow;
                                } else {
                                    currentMonth++;
                                }
                            }
                        }
                    } else {
                        if (currentMonth == monat) {
                            Range rn = mySheet.Cells[currentRow - 1, "M"];
                            if (rn.Value != null) {
                                int newInvNo;
                                if (int.TryParse(rn.Value.ToString(), out newInvNo)) {
                                    NewInvoiceNumber = (maxNr >= newInvNo ? maxNr + 1 : newInvNo + 1);
                                } else {
                                    NewInvoiceNumber = maxNr + 1;
                                }

                            }
                            return currentRow;
                        }
                    }
                    currentRow += 1;
                }
            } catch (Exception ex) {
                //log.Add("GetNextFreeRowNumber (currentRow:" + currentRow.ToString() + ", currentMonth:" + currentMonth.ToString(), ex);
                Debug.Print(ex.Message);
            }

            return ret;
        }

        #endregion

    }
}
